package com.rockcent.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.domain.enumclass.Whether;

import com.rockcent.helper.TreeHelper;
import com.rockcent.mall.lib.domain.AuthResource;
import com.rockcent.mall.lib.domain.AuthUserResource;
import com.rockcent.mall.lib.domain.UserAdmin;
import com.rockcent.mall.lib.domain.UserInfo;
import com.rockcent.mall.lib.domain.enumclass.SystemType;
import com.rockcent.mall.lib.dto.MenuTreeDTO;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.AuthUserHelper;
import com.rockcent.mall.lib.repository.AuthResourceRepository;
import com.rockcent.mall.lib.repository.AuthUserResourceRepository;
import com.rockcent.mall.lib.repository.UserAdminRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by hone on 17-3-13.
 */
@Service
public class RAuthUserServiceImpl extends BaseAuthUserServiceImpl {

    private final Logger log = LoggerFactory.getLogger(RAuthUserServiceImpl.class);

    @Autowired
    private UserAdminRepository userAdminRepository;

    @Autowired
    private AuthUserHelper userHelper;

    @Autowired
    private AuthUserResourceRepository authUserResourceRepository;

    /**
     * 登录
     * @param userInfo
     * @param request
     * @return
     */
    @Override
    public UserInfoDto login(UserInfo userInfo, HttpServletRequest request) {
        UserInfoDto userInfoDto = null;
        if(userInfo==null || userInfo.getId()==null) {
            return userInfoDto;
        }
//        log.info("======================>useradmin userId{}",userInfo.getId());
        UserAdmin userAdmin = userAdminRepository.findByUserId(userInfo.getId());
        if(userAdmin==null) {
            userInfoDto = new UserInfoDto();
            List<String> errors = new ArrayList();
            errors.add("用户没有R端权限");
            userInfoDto.setErrorMessage(errors);
            return userInfoDto;
        }
        if(userAdmin.getAvailable()==null || userAdmin.getAvailable() != Whether.YES) {
            userInfoDto = new UserInfoDto();
            List<String> errors = new ArrayList();
            errors.add("用户未启用");
            userInfoDto.setErrorMessage(errors);
            return userInfoDto;
        }
        List<MenuTreeDTO> menuTreeDTOs = new ArrayList<>();
        if (userAdmin.getIsSuperAdmin()!=null && userAdmin.getIsSuperAdmin() != Whether.YES) {
            //设置REDIS
            //查找用户拥有的资源
            List<AuthUserResource> authUserResources = authUserResourceRepository.findByUserAdminId(userAdmin.getId());
            menuTreeDTOs = getMenuTreeDTOs(authUserResources,userAdmin.getUserId(), SystemType.ADMIN, true, "");
        }
        userInfoDto = userHelper.setSession(userInfo, userAdmin, request);
        userInfoDto.setUsername(userAdmin.getUsername());
        userInfoDto.setResourceList(menuTreeDTOs);
        userInfoDto.setIsSuper(userAdmin.getIsSuperAdmin()==null || userAdmin.getIsSuperAdmin() != Whether.YES?"NO":"YES");
        return userInfoDto;
    }

    @Override
    public UserInfoDto getManager(Long userId, String domain, String userToken, Long merchantId) {
        UserInfoDto userInfoDto = null;
        if(userId==null) {
            return userInfoDto;
        }
        userInfoDto = new UserInfoDto();
        UserAdmin userAdmin = userAdminRepository.findByUserId(userId);
        if (userAdmin == null) {
            List<String> errors = new ArrayList();
            errors.add("用户没有R端权限");
            userInfoDto.setErrorMessage(errors);
            return userInfoDto;
        }
        if(userAdmin.getAvailable()==null || userAdmin.getAvailable() != Whether.YES) {
            List<String> errors = new ArrayList();
            errors.add("用户未启用");
            userInfoDto.setErrorMessage(errors);
            return userInfoDto;
        }
        userInfoDto.setUserAdmin(userAdmin);
        return userInfoDto;
    }
}
